package com.esd.db.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.esd.db.dao.ArticleOpenLogMapper;
import com.esd.db.dao.VtdArticleMapper;
import com.esd.db.dao.VtdCommentMapper;
import com.esd.db.model.ArticleOpenLog;
import com.esd.db.model.VtdArticle;
import com.esd.db.model.VtdComment;
import com.esd.db.service.ArticleService;

@Service("ArticleService")
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	VtdArticleMapper articleMapper;
	@Autowired
	VtdCommentMapper commentMapper;
	@Autowired
	ArticleOpenLogMapper aolMapper;
			
	@Override
	public int saveArticle(VtdArticle article) {
		return articleMapper.insertVtdArticle(article);
	}

	@Override
	public int getWaitInspArticleCount() {
		return articleMapper.getWaitInspArticleCount();
	}

	@Override
	public List<VtdArticle> getArticleListByCondition(String articleTitle, String createUsername, Integer isDelete, 
			Integer isGranted, Integer isLocked, Integer isTop, int page, int row) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (articleTitle.trim().length() > 0) {
			map.put("title", articleTitle);
		} else {
			map.put("title", null);
		}
		if (createUsername.trim().length() > 0) {
			map.put("createUsername", createUsername);
		} else {
			map.put("createUsername", null);
		}
		if(isDelete!=null){
			map.put("isDelete", isDelete);
		}else{
			map.put("isDelete", null);
		}
		if(isGranted!=null){
			map.put("isGranted", isGranted);
		}else{
			map.put("isGranted", null);
		}
		if(isLocked!=null){
			map.put("isLocked", isLocked);
		}else{
			map.put("isLocked", null);
		}
		if(isTop!=null){
			map.put("isTop", isTop);
		}else{
			map.put("isTop", null);
		}
		if (page == 0) {
			map.put("begin", null);
			map.put("end", null);
		} else {
			map.put("begin", (page - 1) * row);
			map.put("end", row);
		}
		return articleMapper.getArticleListByCondition(map);
	}

	@Override
	public int getArticleListByConditionCount(String articleTitle, String createUsername, Integer isDelete,
			Integer isGranted, Integer isLocked, Integer isTop) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (articleTitle.trim().length() > 0) {
			map.put("title", articleTitle);
		} else {
			map.put("title", null);
		}
		if (createUsername.trim().length() > 0) {
			map.put("createUsername", createUsername);
		} else {
			map.put("createUsername", null);
		}
		if(isDelete!=null){
			map.put("isDelete", isDelete);
		}else{
			map.put("isDelete", null);
		}
		if(isGranted!=null){
			map.put("isGranted", isGranted);
		}else{
			map.put("isGranted", null);
		}
		if(isLocked!=null){
			map.put("isLocked", isLocked);
		}else{
			map.put("isLocked", null);
		}
		if(isTop!=null){
			map.put("isTop", isTop);
		}else{
			map.put("isTop", null);
		}
		return articleMapper.getArticleListByConditionCount(map);
	}

	@Override
	public VtdArticle selectById(String id) {
		return articleMapper.selectById(id);
	}

	@Override
	public int changeDeleteStatus(String id, int isDelete, int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("isDelete", isDelete);
		map.put("userId", userId);
		return articleMapper.changeDeleteStatus(map);
	}
	
	@Override
	public int changeInspStatus(String id, int isGranted, int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("isGranted", isGranted);
		map.put("userId", userId);
		return articleMapper.changeInspStatus(map);
	}

	@Override
	public int changeTopStatus(String id, int isTop, int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("isTop", isTop);
		map.put("userId", userId);
		return articleMapper.changeTopStatus(map);
	}
	
	@Override
	public int changeLockStatus(String id, int isLocked, int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("isLocked", isLocked);
		map.put("userId", userId);
		return articleMapper.changeLockStatus(map);
	}

	@Override
	public int managerSaveArticle(VtdArticle article) {
		return articleMapper.managerSaveArticle(article);
	}

	@Override
	public List<VtdArticle> getArticleListByUserId(String title, int userId, int page, int row) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		if(title.trim().length() > 0){
			map.put("title", title);
		}else{
			map.put("title", null);
		}
		if (page == 0) {
			map.put("begin", null);
			map.put("end", null);
		} else {
			map.put("begin", (page - 1) * row);
			map.put("end", row);
		}
		return articleMapper.getArticleListByUserId(map);
	}

	@Override
	public int getArticleListByUserIdCount(int userId, String title) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		if(title.trim().length() > 0){
			map.put("title", title);
		}else{
			map.put("title", null);
		}
		return articleMapper.getArticleListByUserIdCount(map);
	}

	@Override
	public int updateTitleAndContentByArticleId(String articleTitle, String articleContent, String articleId,
			int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", articleTitle);
		map.put("content", articleContent);
		map.put("id", articleId);
		map.put("userId", userId);
		return articleMapper.updateTitleAndContentByArticleId(map);
	}

	@Override
	public int saveComment(VtdComment comment) {
		return commentMapper.insertVtdComment(comment);
	}

	@Override
	public List<VtdComment> findCommentsByArticleId(String articleId) {
		return commentMapper.selectAllByArticleId(articleId);
	}

	@Override
	public VtdComment findCommentById(String id) {
		return commentMapper.selectById(id);
	}
	
	@Override
	public List<VtdComment> findCommentReplysByPid(String pid) {
		return commentMapper.selectAllByPid(pid);
	}

	@Override
	public List<VtdComment> getCommentListForInspection(int page, int row) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (page == 0) {
			map.put("begin", null);
			map.put("end", null);
		} else {
			map.put("begin", (page - 1) * row);
			map.put("end", row);
		}
		return commentMapper.selectCommentListForInspection(map);
	}

	@Override
	public int getCommentListForInspectionCount() {
		return commentMapper.selectCommentListForInspectionCount();
	}

	@Override
	public int deleteComment(String id) {
		return commentMapper.deleteById(id);
	}

	@Override
	public int commentIsGranted(String id) {
		return commentMapper.commentIsGranted(id);
	}

	@Override
	public int getTotleCommentNum(String artecleId) {
		return commentMapper.selectTotleCommentNum(artecleId);
	}

	@Override
	public int getUnreadMsgNumByUserId(int userId) {
		return commentMapper.selectUnreadMsgNumByUserId(userId);
	}

	@Override
	public List<Map<String,Object>> getCommentReplyListByTargetUserId(int targetUserId, int begin) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("targetUserId", targetUserId);
		map.put("begin", begin);
		map.put("end", 10);
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
		List<VtdComment> list = commentMapper.selectByTargetUserId(map);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (VtdComment comment : list) {
			Map<String, Object> tempMap = new HashMap<String, Object>();
			tempMap.put("createUsername", comment.getCreateUsername());
			tempMap.put("articleId", comment.getArticleId());
			tempMap.put("createTime", df.format(comment.getCreateTime()));
			tempMap.put("content", comment.getContent());
			tempMap.put("isCommentReply", comment.getIsCommentReply());
			if(comment.getIsCommentReply()==0){//回复的是文章 使用articleId查询文章
				VtdArticle article = articleMapper.selectById(comment.getArticleId());
				tempMap.put("targetContent", article.getArticleTitle());  //将文章的标题填入
				tempMap.put("targetUsername", "文章");
			}else{ //回复的是评论使用pid查询评论
				VtdComment targetComment = commentMapper.selectById(comment.getPid());
				tempMap.put("targetContent", targetComment.getContent());
				tempMap.put("targetUsername", comment.getTargetUsername());
			}
			resultList.add(tempMap);
		}
		return resultList;
	}

	@Override
	public int getCommentReplyListByTargetUserIdTotle(int targetUserId) {
		return commentMapper.selectCommentReplyListByTargetUserIdCount(targetUserId);
	}

	@Override
	public int changeCommentUnreadStatus(int userId) {
		return commentMapper.updateCommentUnreadStatus(userId);
	}

	@Override
	public int addArticleOpenLog(int userId, String articleId) {
		ArticleOpenLog aol = new ArticleOpenLog();
		aol.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
		aol.setArticleId(articleId);
		aol.setUserId(userId);
		return aolMapper.insertVtdArticle(aol);
	}

	@Override
	public Map<String, Object> queryOpenLogNumAndCommentNumByArticleId(String articleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		int commentNum = commentMapper.selectTotleCommentNum(articleId);
		int openLogNum = aolMapper.selectTotleNumByArticleId(articleId);
		map.put("commentNum", commentNum);
		map.put("openLogNum", openLogNum);
		return map;
	}

	@Override
	public List<Map<String,Object>> getArticleOpenLogListByUserId(int userId, int page, int row) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (page == 0) {
			map.put("begin", null);
			map.put("end", null);
		} else {
			map.put("begin", (page - 1) * row);
			map.put("end", row);
		}
		map.put("userId", userId);
		return aolMapper.selectArticleOpenLogInfoListByUserId(map);
	}

	@Override
	public int articleOpenLogInfoListCount(int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId); 
		return aolMapper.selectArticleOpenLogInfoListByUserIdCount(map);
	}

	@Override
	public List<Map<String, Object>> getAllArticleOpenLogListForOverView(int page, int row) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (page == 0) {
			map.put("begin", null);
			map.put("end", null);
		} else {
			map.put("begin", (page - 1) * row);
			map.put("end", row);
		}
		return aolMapper.selectAllArticleOpenLogInfoList(map);
	}

	@Override
	public int getAllArticleOpenLogListForOverViewCount() {
		return aolMapper.selectAllArticleOpenLogInfoListCount();
	}
	
}
